﻿using log4net;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Quartz.NET.Practice
{
    public  class LogService
    {

        IScheduler _scheduler;

        public async  void OnStart()
        {
            var schedulerFactory = new StdSchedulerFactory();
            _scheduler = await schedulerFactory.GetScheduler();
            await _scheduler.Start();

            IJobDetail job1 = JobBuilder.Create<LogJob>().WithIdentity("Job1", "Group1").Build();

            ITrigger trigger1 = 
                TriggerBuilder.Create().WithIdentity("Trigger1", "Group1").WithCronSchedule("0/10 * * * * ?").Build();

            await _scheduler.ScheduleJob(job1, trigger1);
            await _scheduler.Start();
            
            
        }

        public void OnStop()
        {
            _scheduler.Shutdown();
        }

        //特定任务
        public class LogJob : IJob
        {
            public static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(LogJob));

            public async Task Execute(IJobExecutionContext context)
            {

                await Task.Factory.StartNew(() =>
                {
                    // File.AppendAllText(@"D:\ServiceLog.txt",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);
                    
                    log.Info("任务执行:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                });

            }
        }
    }
}
